//
//  CZEmoticonToolBar.swift
//  Weibo27
//
//  Created by apple on 16/4/25.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
// 设置button tag 使用
let tagNum = 2000
// 底部toolbar 枚举
enum CZEmoticonToolBarType: Int{
    // 默认表情
    case Default = 0
    // Emoji表情
    case Emoji = 1
    // 浪小花
    case Lxh = 2
}


class CZEmoticonToolBar: UIView {
    
    var emoticonToolBarClosure: ((type: CZEmoticonToolBarType)->())?
    
    // 记录上次点击的按钮
    var currentButton: UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setEmoticonCurrentButton(section: Int){
        // 通过tag 获取指定的按钮
        let button = viewWithTag(section + tagNum) as! UIButton
        
        
        if button == currentButton{
            return
        }
        button.selected = true
        // 取消上次点击按钮的状态
        currentButton?.selected = false
        // 保存这次点击的按钮
        currentButton = button
    }
    
    // MARK: - 监听方法
    func buttonClick(button: UIButton){
        
        if button == currentButton{
            return
        }
        button.selected = true
        // 取消上次点击按钮的状态
        currentButton?.selected = false
        // 保存这次点击的按钮
        currentButton = button
        
        // 执行闭包
        emoticonToolBarClosure?(type: CZEmoticonToolBarType(rawValue: button.tag - tagNum)!)
    }
    
    
    // MARK: - 设置视图
    private func setupUI(){
        self.backgroundColor = RandomColor()

        let defaultButton = addChildButton("compose_emotion_table_left", title: "默认", type: .Default)
        let emojiButton = addChildButton("compose_emotion_table_mid", title: "Emoji", type: .Emoji)
        let lxhButton = addChildButton("compose_emotion_table_right", title: "浪小花", type: .Lxh)
        
        
        // 添加约束
        defaultButton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(emojiButton)
        }
        
        emojiButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(defaultButton.snp_trailing)
            make.width.equalTo(lxhButton)
        }
        
        lxhButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emojiButton.snp_trailing)
        }
        
        // 设置默认按钮选中状态
        defaultButton.selected = true
        
        currentButton = defaultButton
        
    }
    
    
    func addChildButton(imgName: String, title: String, type: CZEmoticonToolBarType) -> UIButton{
        let button = UIButton()
        button.tag = type.rawValue + tagNum
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "\(imgName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(imgName)_selected"), forState: UIControlState.Selected)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        addSubview(button)
        return button
    }
    
}
